package com.fivefu.core.camera.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.fileupload.minio.services.MinioServices;
import com.fivefu.base.web.controller.BaseController;
import com.fivefu.base.web.vo.ResultInfo;
import com.fivefu.core.camera.entity.DbMonitorLabel;
import com.fivefu.core.camera.entity.DbMonitorStatus;
import com.fivefu.core.camera.services.RegionService;
import com.fivefu.core.camera.services.DbMonitorLabelService;
import com.fivefu.core.camera.services.DbMonitorStatusService;
import com.fivefu.core.camera.utils.FileUploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 公用接口 前端控制器
 * </p>
 *
 * @author
 * @since 2022-09-19 19:38:06
 */
@Api(tags = "公用接口管理")
@RestController
@RequestMapping("/common")
public class DbCommonController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(DbEventController.class);

    @Autowired
    private RegionService regionService;

    @Autowired
    private DbMonitorLabelService dbMonitorLabelService;

    @Autowired
    private DbMonitorStatusService dbMonitorStatusService;

    @Autowired
    private MinioServices minioServices;

    @RequestMapping(path = "/queryAreaList",method = RequestMethod.GET)
    @ApiOperation(value = "城区列表",httpMethod = "GET",response = ResultInfo.class,notes = "城区列表")
    public ResultInfo queryAreaList(){
        try {
            List<Map<String, String>> maps = regionService.queryAreaList();
            return ResultInfo.renderSuccess(maps);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询城区列表失败");
    }

    @RequestMapping(path = "/queryStreetListByRegionCode",method = RequestMethod.POST)
    @ApiOperation(value = "街道列表",httpMethod = "POST",response = ResultInfo.class,notes = "街道列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "regionCode", value = "城区编号", paramType = "query",required = true, dataType = "String"),
    })
    public ResultInfo getAreaList(String regionCode){
        try {
            if (StrUtils.isEmpty(regionCode)){
                return ResultInfo.renderError("城区编号不能为空");
            }
            List<Map<String, String>> maps = regionService.queryStreetListByRegionCode(regionCode);
            return ResultInfo.renderSuccess(maps);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询街道列表失败");
    }

    @RequestMapping(path = "/queryLabelList",method = RequestMethod.POST)
    @ApiOperation(value = "标签列表",httpMethod = "POST",response = ResultInfo.class,notes = "标签列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query",required = false, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "数量", paramType = "query",required = false, dataType = "int"),
    })
    public ResultInfo queryLabelList(Integer page,Integer limit){
        try {
            Page<DbMonitorLabel> labelPage = dbMonitorLabelService.queryLabelList(page, limit);
            return ResultInfo.renderSuccess(200,"查询事件管理列表成功",labelPage.getTotal(),labelPage.getRecords());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询标签列表失败");
    }

    @RequestMapping(path = "/queryStatusList",method = RequestMethod.POST)
    @ApiOperation(value = "状态列表",httpMethod = "POST",response = ResultInfo.class,notes = "状态列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "statusType", value = "状态类型(examine : 审核,event : 案卷状态,push : 案卷推送状态)", paramType = "query",required = true, dataType = "String"),
    })
    public ResultInfo queryStatusList(String statusType){
        try {
            logger.info("查询状态列表接收数据：" + JSON.toJSONString(request.getParameterMap()));
            if (StrUtils.isNull(statusType)){
                return ResultInfo.renderError("状态类型不能为空");
            }
            List<DbMonitorStatus> list = dbMonitorStatusService.queryMonitorStatusByType(statusType);
            return ResultInfo.renderSuccess(list);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询状态列表失败");
    }

    @ApiOperation(value="上传图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imgurl", value = "图片地址", paramType = "query", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public ResultInfo uploadImg(String imgurl, @RequestAttribute String userid){
        logger.info("上传图片接收的参数为：" + JSON.toJSONString(request.getParameterMap()));
        try {
            if (StrUtils.isNull(imgurl)){
                return ResultInfo.renderError("图片地址不能为空");
            }
            String file = "";
            if(StrUtils.isNotNull(imgurl)){
                String suffix = imgurl.substring(imgurl.indexOf("/")+1, imgurl.indexOf(";"));
                imgurl = imgurl.substring(19 + suffix.length());
                InputStream inputStream = FileUploadUtils.BaseToInputStream(imgurl);
                file = minioServices.fileUpload(inputStream, "base64." + suffix,  "img/" + userid, null);
            }
            return ResultInfo.renderSuccess(file);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return ResultInfo.renderError("上传图片失败");
        }
    }
}
